import {Component} from '@angular/core';
import {AlertController, IonicPage,MenuController, NavController} from 'ionic-angular';


import {RetrievePasswordPage} from '../../../pages/retrieve-password/retrieve-password';
import {EmployeeHomePage} from '../home/home';
import {CustomerLoginPage} from '../../../customer/pages/customer-login/customer-login';

import {EmployeeProvider} from '../../../providers/employeeProvider';
/**
 * Generated class for the EmployeeLoginPage page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */
@IonicPage()
@Component({
  selector: 'page-employee-login',
  templateUrl: 'employee-login.html',
})
export class EmployeeLoginPage {
  employeeLoginId: string;
  employeePassword: string;

  constructor(public navCtrl: NavController,
              public auth: EmployeeProvider,
              public menuCtrl:MenuController,

              public alert: AlertController) {
  }

  login() {
    if (this.employeeLoginId == null || this.employeePassword == null || this.employeeLoginId == '' || this.employeePassword == '') {
      this.alertMsg('Error', 'Username Or password can\'t be null!');
      return false;
    }
    this.auth.login(this.employeeLoginId, this.employeePassword).subscribe(res => {
      if (res.success) {
        this.auth.store(res.data.Employee_Store);
        this.navCtrl.setRoot(EmployeeHomePage);
        this.menuCtrl.enable(true,'employeeMenuID');
        this.menuCtrl.enable(false,'customerMenuID');

      }
      else
        this.alertMsg('error', res.message);
    });
    /*if(! this.auth.login(this.employeeLoginId, this.employeePassword))
     {
     this.alertMsg('Error','Credentials invalid!');
     return false;
     }

     this.navCtrl.setRoot(EmployeeHomePage);*/
  }

  private alertMsg(title: string, subTitle: string) {
    let alert = this.alert.create({
      title: title,
      subTitle: subTitle,
      buttons: ['OK']
    });
    alert.present();
  }

  pushPage(retireve) {
    this.navCtrl.push(RetrievePasswordPage, {});
  }

  customer() {
    this.navCtrl.setRoot(CustomerLoginPage)
  }


  ionViewDidLoad() {
    console.log('ionViewDidLoad EmployeeLoginPage');
  }

}
